
var URL = '/ctg-sysmgr-permission-demo/';
var app = angular.module('auth', ['ui.router','pasp.ui']);

app.config(function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/grant/comp');
    $stateProvider
        .state({
            name: 'login',
            url: '/login',
            template:  "<div class=\"login\">"
            + "    <h1>权限管理</h1>"
            + "    <div class=\"row\">"
            + "        <div class=\"col-sm-12\">"
            + "            <div class=\"form\">"
            + "                <div class=\"form-group\">"
            + "                    <label for=\"username\">用户名</label>"
            + "                    <input type=\"text\" id=\"username\" ng-model=\"vm.data.username\" class=\"form-control\" required>"
            + "                </div>"
            + "                <div class=\"form-group\">"
            + "                    <label for=\"password\">密码</label>"
            + "                    <input type=\"password\" id=\"password\" ng-model=\"vm.data.password\" class=\"form-control\" required>"
            + "                </div>"
            + "                <div class=\"form-group\">"
            + "                    <button class=\"btn btn-primary\" ng-click=\"vm.entry()\">登录</button>"
            + "                </div>"
            + "            </div>"
            + "        </div>"
            + "    </div>"
            + "</h1>",
            controllerAs: 'vm',
            controller: function ($scope, $http, $state) {
                this.entry = function() {
                    console.log(this.data)
                    //$http.post('/someUrl', data, config).then(successCallback, errorCallback);
                    var config = {
                        method: 'POST',
                        url: URL + 'login',
                        headers: {
                            'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
                        },
                        data: parseParams(this.data)
                    }
                    $http(config).then(function(r){
                        if (r.status === 200) {
                            $http.get(URL + 'perm/permission').then(function(r){
                            	console.log(r.data)
                                if (r.status === 200) {
                                	console.log(r.data)
                                    window.res_data = r.data.data.menus;
                                    window.permission = r.data.data.comps;
                                }
                                $state.go('grant.comp')
                            })
                        }
                    })
                }
            }
        })
        .state({
            name: 'grant',
            url: '/grant',
            template: "<div class=\"wrap-in row\">"
                + "    <div class=\"col-sm-3\">"
                + "        <div class=\"list-group\">"
                + "            <a ui-sref=\"{{item.url_addr}}\" ng-repeat=\"item in vm.menus track by $index\" ui-sref-active=\"active\" class=\"list-group-item\">"
                + "                {{item.menu_name}}"
                + "            </a>"
                + "        </div>"
                + "    </div>"
                + "    <div class=\"col-sm-9\">"
                + "        <ui-view></ui-view>"
                + "    </div>"
                + "</div>",
            controllerAs: 'vm',
            controller: function () {
                this.menus = window.res_data || []
                console.log(this.menus)
                console.log(window.res_data)
            }
        })
        .state({
            name: 'grant.comp',
            url: '/comp',
            template: "<button class=\"btn btn-primary\" ui-permission=\"btn_add\">新增用户</button><button class=\"btn btn-primary\" ui-permission=\"btn_del\">删除用户</button><button class=\"btn btn-primary\" ui-permission=\"btn_edit\">修改用户</button>"
        })
        .state({
            name: 'grant.table',
            url: '/table',
            template: "<table class=\"table table-bordered\">"
            + "    <thead>"
            + "        <tr>"
            + "            <th>名称</th>"
            + "            <th>编码</th>"
            + "            <th>操作</th>"
            + "        </tr>"
            + "    </thead>"
            + "    <tbody>"
            + "        <tr>"
            + "            <td>test1</td>"
            + "            <td>test1</td>"
            + "            <td><button class=\"btn btn-primary btn-xs\" ui-permission=\"123\" ui-permission-disable=\"true\">I am disabled</button></td>"
            + "        </tr>"
            + "        <tr>"
            + "            <td>test1</td>"
            + "            <td>test1</td>"
            + "            <td><button class=\"btn btn-primary btn-xs\" ui-permission=\"ok\">I am ok</button></td>"
            + "        </tr>"
            + "    </tbody>"
            + "</table>"
        })
})

function parseParams (param) {
    if (Object.prototype.toString.call(param) !== '[object Object]') return;
    var r = [];
    for (var i in param) {
        r.push(i + '=' + encodeURIComponent(param[i]))
    }
    return r.join('&')
}
